//index.js
//获取应用实例
const app = getApp();
let pNo = 2, gId = '', ris = true;
const getToken = require('../../utils/get_token.js');
const formatTime = require('../../utils/util.js');

let openId = '', memberId = '';
let cookie, token;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  first(that);
  indexList(that)//初始加载
}
const first = (that) => {//商品列表
  let params = [ 'shop/pingTuan/gtli', {"tuanType":1,"pageNo":1}]
  getToken.getData(function (res) {
    let data = res.data;
    console.log(data)
    if (data.rows.length > 0) {
      for (var i = 0; i < data.rows.length; i++) {
        data.rows[i].endTime = new Date(data.rows[i].endTime);
        data.rows[i].endTime = formatTime.formatTime(data.rows[i].endTime);
        data.rows[i].endTime = data.rows[i].endTime.substring(5, 16);
       // data.rows[i].imageStore = data.rows[i].imageStore.substring(1, data.rows[i].imageStore.length - 1);
        data.rows[i].imageStore = JSON.parse(data.rows[i].imageStore);
        data.rows[i].img = 'https://www.globalhexi.com/' + data.rows[i].imageStore[0].path + '/small/' + data.rows[i].imageStore[0].id + '.jpg';
        for(let j=0;j<data.rows[i].memLogoList.length;j++)
						{  
							if(data.rows[i].memLogoList[j].indexOf('http')==-1)
							{
								data.rows[i].memLogoList[j]='http://www.globalhexi.com/'+data.rows[i].memLogoList[j];
							}
						}
        data.rows[i].mList = data.rows[i].memLogoList.slice(0, 2);
        that.data.list.push(data.rows[i]);
      }
    }
    that.setData({
      list: that.data.list
    })
  }, ...params)
};
const indexList = (that) => {//商品列表
  if (that.data.boolT) {
    that.setData({
      hidden: false
    })
    that.data.boolT = false;
    let params = ['shop/pingTuan/gtli', {"tuanType":1,"pageNo": pNo}]
    getToken.getData(function (res) {
      let data = res.data;
      if (data.rows.length > 0) {
        for (var i = 0; i < data.rows.length; i++) {
          data.rows[i].endTime = new Date(data.rows[i].endTime);
          data.rows[i].endTime = formatTime.formatTime(data.rows[i].endTime);
          data.rows[i].endTime = data.rows[i].endTime.substring(5, 16);
          //data.rows[i].imageStore = data.rows[i].imageStore.substring(1, data.rows[i].imageStore.length - 1);
          data.rows[i].imageStore = JSON.parse(data.rows[i].imageStore);
          data.rows[i].img = 'http://www.globalhexi.com/' + data.rows[i].imageStore[0].path + '/small/' + data.rows[i].imageStore[0].id + '.jpg';
          data.rows[i].mList = data.rows[i].memLogoList.slice(0, 2);
          that.data.list.push(data.rows[i]);
        }
        pNo++;
      } 
      setTimeout(function () {
        that.data.boolT = true;
        that.setData({
          list: that.data.list,
          hidden: true
        })
      }, 500)
    }, ...params)
  }
};

Page({
  data: {
    hidden: false,
    boolT: true,
    list: []
  },
  onLoad: function () {
    let that = this;
    memberId = wx.getStorageSync('memberId') || '';
    openId = wx.getStorageSync('openId') || '';
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    pNo = 2;
    ckToken(that);
  },
  go_detail: function (e) {//直接去详情
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../paired_detail/paired_detail?scene=' + e.currentTarget.dataset.id,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  go_split: function (e) {//伙拼
    let that = this;
    let id = e.currentTarget.dataset.id;
    let td = e.currentTarget.dataset.td;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../tuxedo/tuxedo?id=' + id + '&tuanid=' + td,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },

  onReachBottom: function () {//上啦加载更多
    let that = this;
    indexList(that);
  },
  onPullDownRefresh: function (e) {//下拉刷新
    let that = this;
    wx.showNavigationBarLoading()//加载动画
    pNo = 1;
    that.data.list = [];
    indexList(that);
    setTimeout(function () {
      wx.hideNavigationBarLoading()//隐藏动画
      wx.stopPullDownRefresh()//停止加载
    }, 500)
  }
})
